from django.contrib import admin
from .models import *

# Register your models here.

admin.AdminSite.site_header = '移动电商管理系统'
admin.AdminSite.site_title = '移动电商管理系统'


class BannerAdmin(admin.ModelAdmin):
    # list_display这个属性即使用于定义人员列表页显示哪些字段，列表页中的值，必须和model类中声明的字段保持一致。
    list_display = ['carousel_id', 'carousel_url', 'redirect_url', 'carousel_rank', 'is_deleted', 'create_time']

    # dis_publish 是自定义的函数，显示的是外键表中的字段名称
    # list_display = ['id','title', 'dis_publish','price', 'pub_date',]

    # 这个fields字段作用域model的添加页面，显示哪些字段可以用于输入内容，不在列表中的数据，默认添加页面就不在显示了。
    # fields = ['p_name']

    # fields属性和fieldsets属性不能同时使用。因为都作用于添加页面。
    fieldsets = [
        ('轮播图', {'fields': ['carousel_url']}),
        ('链接地址', {'fields': ['redirect_url']}),
        ('轮播排序', {'fields': ['carousel_rank']}),
        ('是否删除', {'fields': ['is_deleted']}),
    ]

    # 针对人员列表页的一个属性配置，在列表页的右侧会出现一个过滤器，可以根据人员的年龄和性别对列表页的人员进行筛选。
    # list_filter = ['price', 'pub_date']

    # # 在人员的列表页顶部会出现一个搜索框。只能根据search_fields内部定义的字段值进行搜索。
    # search_fields = ['title','price']

    # list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 10

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('carousel_id',)

    # list_editable 设置默认可编辑字段
    # list_editable = ['title']

    # fk_fields 设置显示外键字段
    # fk_fields = ('publish_id',)

    # 写一个方法,定义在管理页面上能够显示的外键字段字段
    # grade为Students模型的外检表,level为Grades模型的外检表,那么为Level模型的字段
    # def dis_publish(self):
    #     return self.publish_id.name

    # 定义该字段在管理后台显示的名称
    # dis_publish.short_description = '出版社名称'

    # 方法列是不能排序的，如果需要排序需要为方法指定排序依据。添加的是'模型类字段'
    # 如果是外键需要遵循这样的语法：本表外键字段__(双下划线)外检表字段或外检表的外键字段__最终外键表要显示的字段。
    # dis_publish.admin_order_field = 'publish_id__name'


class MallUserAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['user_id', 'phone_num', 'login_name', 'nick_name', 'user_pwd', 'locked', 'introduce', 'is_deleted', 'create_time']

    # 哪些字段 点击链接进入编辑界面
    list_display_links = ['user_id']

    # 可编辑哪几个字段
    # list_editable = ['login_name', 'nick_name', 'introduce', 'locked', 'is_deleted']

    # 添加页面显示的字段
    fieldsets = [
        ('用户ID', {'fields': ['user_id']}),
        ('用户手机号码', {'fields': ['phone_num']}),
        ('用户名', {'fields': ['login_name']}),
        ('昵称', {'fields': ['nick_name']}),
        ('密码', {'fields': ['user_pwd']}),
        ('锁定状态', {'fields': ['locked']}),
        ('个人简介', {'fields': ['introduce']}),
        ('是否删除', {'fields': ['is_deleted']}),
    ]
    # 过滤器
    list_filter = ['is_deleted', 'locked']

    # 搜索栏
    search_fields = ['user_id', 'login_name']

    # 分页
    list_per_page = 10


class UserAddressAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['address_id', 'user_id', 'consignee', 'consignee_phone', 'default_flag', 'province_name',
                    'city_name', 'region_name', 'detail_address', 'create_time', 'update_time']
    # 哪些字段 点击链接进入编辑界面
    list_display_links = ['address_id']

    # 添加页面显示的字段
    fieldsets = [
        ('用户ID', {'fields': ['user_id']}),
        ('收货人姓名', {'fields': ['consignee']}),
        ('收货人电话', {'fields': ['consignee_phone']}),
        ('是否默认地址', {'fields': ['default_flag']}),
        ('省', {'fields': ['province_name']}),
        ('市', {'fields': ['city_name']}),
        ('地区', {'fields': ['region_name']}),
        ('详细地址', {'fields': ['detail_address']})
    ]
    # 搜索栏
    search_fields = ['user_id', 'consignee']

    # 分页
    list_per_page = 10

class GoodsInfoAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['goods_id', 'goods_name', 'goods_intro', 'goods_category_id', 'goods_cover_img', 'goods_carousel',
                    'goods_detail_content', 'original_price', 'selling_price', 'stock_num', 'tag', 'goods_sell_status', 'create_user', 'create_time', 'update_time', 'update_user']

    # 添加页面显示的字段
    fieldsets = [
        ('商品名称', {'fields': ['goods_name']}),
        ('商品介绍', {'fields': ['goods_intro']}),
        ('商品分类ID', {'fields': ['goods_category_id']}),
        ('商品封面图', {'fields': ['goods_cover_img']}),
        ('商品滚屏', {'fields': ['goods_carousel']}),
        ('商品详情内容', {'fields': ['goods_detail_content']}),
        ('原价', {'fields': ['original_price']}),
        ('售价', {'fields': ['selling_price']}),
        ('库存数量', {'fields': ['stock_num']}),
        ('标签', {'fields': ['tag']}),
        ('商品销售状态', {'fields': ['goods_sell_status']}),
        ('创建用户', {'fields': ['create_user']}),
        ('更新用户', {'fields': ['update_user']}),
    ]
    # 搜索栏
    search_fields = ['goods_name', 'user_id']

    # 过滤器
    list_filter = ['goods_category_id', 'stock_num', 'tag']

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('goods_id',)

    # 分页
    list_per_page = 10

class GoodsCategoryAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['category_id', 'category_level', 'parent_id', 'category_name', 'category_rank', 'is_deleted', 'create_user',
                    'create_time', 'update_time', 'update_user']

    # 添加页面显示的字段
    fieldsets = [
        ('分类ID', {'fields': ['category_id']}),
        ('分类等级', {'fields': ['category_level']}),
        ('父类ID', {'fields': ['parent_id']}),
        ('分类名称', {'fields': ['category_name']}),
        ('分类排名', {'fields': ['category_rank']}),
        ('是否删除', {'fields': ['is_deleted']}),
        ('创建用户', {'fields': ['create_user']}),
        ('更新用户', {'fields': ['update_user']}),
    ]
    # 搜索栏
    search_fields = ['category_level', 'parent_id', 'category_name', 'is_deleted']

    # 过滤器
    list_filter = ['category_level', 'category_name', 'is_deleted']

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('category_id',)

    # 分页
    list_per_page = 10

class ShoppingCartAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['cart_item_id', 'user_id', 'goods_id', 'goods_count', 'unit_price', 'is_deleted']

    # 添加页面显示的字段
    fieldsets = [
        # ('购物车选项ID', {'fields': ['cart_item_id']}),
        ('用户ID', {'fields': ['user_id']}),
        ('商品ID', {'fields': ['goods_id']}),
        ('商品数量', {'fields': ['goods_count']}),
        ('商品单价', {'fields': ['unit_price']}),
        ('是否删除', {'fields': ['is_deleted']}),
    ]
    # 搜索栏
    search_fields = ['goods_id']

    # 过滤器
    list_filter = ['user_id', 'goods_id', 'is_deleted']

    # 分页
    list_per_page = 10

class MallOrderAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['order_nu', 'user_id', 'total_price', 'pay_status', 'pay_type', 'pay_time',
                    'order_status', 'extra_info', 'is_deleted', 'create_time', 'update_time']

    # 添加页面显示的字段
    fieldsets = [
        # ('订单号', {'fields': ['order_id']}),
        ('订单编号', {'fields': ['order_nu']}),
        ('用户ID', {'fields': ['user_id']}),
        ('总价', {'fields': ['total_price']}),
        ('支付状态', {'fields': ['pay_status']}),
        ('支付类型', {'fields': ['pay_type']}),
        ('订单状态', {'fields': ['order_status']}),
        ('备注', {'fields': ['extra_info']}),
        ('是否删除', {'fields': ['is_deleted']}),
    ]
    # 搜索栏
    search_fields = ['order_nu', 'user_id', 'pay_status', 'order_status']

    # 过滤器
    list_filter = ['pay_status', 'order_status', 'is_deleted']

    # 分页
    list_per_page = 10

class OrderItemAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['order_item_id', 'order_nu', 'goods_id', 'goods_name', 'goods_cover_img',
                    'selling_price', 'goods_count', 'create_time']

    # 添加页面显示的字段
    fieldsets = [
        ('订单项ID', {'fields': ['order_item_id']}),
        ('订单编号', {'fields': ['order_nu']}),
        ('商品ID', {'fields': ['goods_id']}),
        ('商品名称', {'fields': ['goods_name']}),
        ('商品封面图', {'fields': ['goods_cover_img']}),
        ('售价', {'fields': ['selling_price']}),
        ('商品数量', {'fields': ['goods_count']}),
    ]
    # 搜索栏
    search_fields = ['order_item_id', 'order_nu']

    # 过滤器
    list_filter = ['order_nu']

    # 分页
    list_per_page = 10

class OrderAddressAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['order_address_id', 'order_nu', 'user_name', 'user_phone', 'province_name', 'city_name', 'region_name',
                    'detail_address']

    # 添加页面显示的字段
    fieldsets = [
        ('订单地址编号', {'fields': ['order_address_id']}),
        ('订单编号', {'fields': ['order_nu']}),
        ('收货人姓名', {'fields': ['user_name']}),
        ('收货人电话', {'fields': ['user_phone']}),
        ('省', {'fields': ['province_name']}),
        ('市', {'fields': ['city_name']}),
        ('区', {'fields': ['region_name']}),
        ('详细地址', {'fields': ['detail_address']})
    ]
    # 搜索栏
    search_fields = ['user_id', 'order_nu']

    # 分页
    list_per_page = 10

class MallCommentAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['comment_id', 'goods_id', 'user_id', 'comment', 'create_time']

    # 添加页面显示的字段
    fieldsets = [
        ('评论ID', {'fields': ['comment_id']}),
        ('商品ID', {'fields': ['goods_id']}),
        ('用户ID', {'fields': ['user_id']}),
        ('评论内容', {'fields': ['comment']}),
    ]
    # 搜索栏
    search_fields = ['comment_id', 'user_id']

    # 分页
    list_per_page = 10

class GridImgAdmin(admin.ModelAdmin):
    # 列表页显示的字段
    list_display = ['grid_id', 'img_name', 'img_url', 'is_deleted', 'create_time']

    # 添加页面显示的字段
    fieldsets = [
        ('名称', {'fields': ['img_name']}),
        ('路由地址', {'fields': ['img_url']}),
        ('是否删除', {'fields': ['is_deleted']}),
    ]
    # 搜索栏
    search_fields = ['img_name', 'is_deleted']

    # 分页
    list_per_page = 10


# admin.site.register(Book,BookAdmin)
admin.site.register(Banner, BannerAdmin)
admin.site.register(MallUser, MallUserAdmin)
admin.site.register(UserAddress, UserAddressAdmin)

admin.site.register(GoodsInfo, GoodsInfoAdmin)
admin.site.register(GoodsCategory, GoodsCategoryAdmin)
admin.site.register(ShoppingCart, ShoppingCartAdmin)
admin.site.register(MallOrder, MallOrderAdmin)
admin.site.register(OrderItem, OrderItemAdmin)
admin.site.register(OrderAddress, OrderAddressAdmin)
admin.site.register(MallComment, MallCommentAdmin)
admin.site.register(GridImg, GridImgAdmin)
